using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;

namespace Zune.Forms
{
    internal class MessageBox : Control
    {
        internal delegate void OnMessageBoxClosed(object sender, Enumerations.DialogResults result);
        internal event OnMessageBoxClosed MessageBoxClosed;

        private Button _yes;
        private Button _cancel;
        private string _text;
        private Vector2 _textSize;
        private float _textY;
        private SpriteFont _font;
        private Enumerations.MessageBoxTypes _type = Enumerations.MessageBoxTypes.Ok;

        internal MessageBox(Form form, bool isSystem) : base(form, isSystem)
        {
            base.BackColor = Color.White;
            base.ForeColor = Color.Black;
            base.Visible = false;
            base.Size = new Microsoft.Xna.Framework.Vector2(222, 150);
            base.Location = new Microsoft.Xna.Framework.Vector2(25, 100);
            _yes = new Button(this);
            _yes.Clicked += new OnControlTouched(button_Clicked);
            _cancel = new Button(this);
            _cancel.Clicked += new OnControlTouched(button_Clicked);
            base._borderType = Enumerations.BorderType.Border3D;
        }

        public override void LoadContent()
        {
            _font = _form.Content.Load<SpriteFont>("DefaultFont");
            base.LoadContent();
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (base.Visible)
            {
                base.Draw(gameTime);
                List<string> lines = Utilities.GetLinesFromString(_font, _text, new Vector2(_location.X + 5, _location.Y + 5), new Vector2(_size.X - 10, _size.Y - 10));
                float tempY = _location.Y + 5;
                for (int i = 0; i < lines.Count; i++)
                {
                    _form.SpriteBatch.DrawString(_font, lines[i], new Vector2(_location.X + 7, tempY), Color.Black);
                    tempY += _textSize.Y;
                }
            }
        }

        public void Show(string text, Enumerations.MessageBoxTypes type)
        {
            _text = text;
            _textSize = _font.MeasureString(_text);
            _type = type;
            base.Visible = true;
            int lineCount = Utilities.GetLinesFromString(_font, _text, new Vector2(_location.X + 5, _location.Y + 5), new Vector2(_size.X - 10, _size.Y - 10)).Count;
            _textY = lineCount * _textSize.Y;
            base.Size = new Vector2(222, _yes._size.Y + _textY + 30);
            base.Location = new Vector2(25, (480 / 2) - (base.Size.Y / 2));
            switch (_type)
            {
                case Enumerations.MessageBoxTypes.Ok:
                    _yes.Visible = true;
                    _yes.Text = "Ok";
                    _yes.Location = new Vector2(_location.X + ((base.Size.X / 2) - (_yes._size.X / 2)), _location.Y + _size.Y - _yes._size.Y - 10);
                    _cancel.Visible = false;
                    break;
                case Enumerations.MessageBoxTypes.OkCancel:
                    float widthX = _yes.Size.X + 5 + _cancel.Size.X;
                    _yes.Visible = true;
                    _yes.Text = "Ok";
                    _yes.Location = new Vector2(_location.X + ((base.Size.X / 2) - (widthX / 2)), _location.Y + _size.Y - _yes._size.Y - 10);
                    
                    _cancel.Visible = true;
                    _cancel.Text = "Cancel";
                    _cancel.Location = new Vector2(_yes.Location.X + _yes.Size.X + 5, _location.Y + _size.Y - _yes._size.Y - 10);
                    break;
                case Enumerations.MessageBoxTypes.YesNo:
                    float widthX2 = _yes.Size.X + 5 + _cancel.Size.X;
                    _yes.Visible = true;
                    _yes.Text = "Yes";
                    _yes.Location = new Vector2(_location.X + ((base.Size.X / 2) - (widthX2 / 2)), _location.Y + _size.Y - _yes._size.Y - 10);

                    _cancel.Visible = true;
                    _cancel.Text = "No";
                    _cancel.Location = new Vector2(_yes.Location.X + _yes.Size.X + 5, _location.Y + _size.Y - _yes._size.Y - 10);
                    break;
            }
        }

        private void button_Clicked(object sender, GameTime gameTime, Microsoft.Xna.Framework.Input.TouchLocation touch)
        {
            base.Visible = false;

            if (MessageBoxClosed != null)
            {
                string buttonText = ((Button)sender).Text;
                Enumerations.DialogResults result = Enumerations.DialogResults.Ok;

                switch (buttonText)
                {
                    case "Yes":
                        result = Enumerations.DialogResults.Yes;
                        break;
                    case "Ok":
                        result = Enumerations.DialogResults.Ok;
                        break;
                    case "Cancel":
                        result = Enumerations.DialogResults.Cancel;
                        break;
                    case "No":
                        result = Enumerations.DialogResults.No;
                        break;
                }

                MessageBoxClosed(this, result);
            }
        }
    }
}